﻿#region Legal

// Jimmy Zimmerman
// Team Mongoose
//
// END USER LICENSE AGREEMENT
// IMPORTANT - READ THIS CAREFULLY:  This End User License Agreement is a legal agreement
// between you (either an individual, business entity, currently active identity of an
// individual with Multiple Personality Disorder, octopus overlord, or otherworldly entity),
// and Team Mongoose, for the enclosed, components.
//
// By reading this document and/or installing this product, you agree to be bound by the
// terms of this EULA.
//
// Team Mongoose owns all copyright, trade secret, trademark, trade wars,
// patent, portent, and potential rights to these components.  Team Mongoose
// grants you the right to deploy the enclosed components.
//
// If you agree to the terms of this EULA, a license to use these components is granted to you.
//
// If you should happen to benefit from the enclosed components, then you are legally and
// contractually bound to thank us for it. Send your regards to jimmyzimms@hotmail.com.
//
// OTHER RIGHTS AND LIMITATONS
// You may not reverse-engineer, decompile, decode, disassemble, psycho-analyze, or otherwise
// attempt to find hidden meanings between the lines of this EULA; unless, of course, you should
// happen to find some, and they are funny.
//
// You may not translate this EULA or any part of the components into Ancient Sumerian.
//
// THESE COMPONENTS ARE PROVIDED “AS-IS” WITHOUT WARRANTY OF ANY KIND. ANY USE OF THE COMPONENTS
// CONTAINED IS AT YOUR OWN RISK. TEAM MONGOOSE DISCLAIM ALL WARRANTIES, EITHER
// EXPRESS OR IMPLIED, WITH RESPECT TO THE ACCURRACY AND CORRECTNESS OF THE COMPONENTS CONTAINED
// HEREIN. TEAM MONGOOSE DOES NOT WARRANT THAT THE COMPONENTS ARE FLAWLESS.
//
// REDISTRIBUTION AND USE IN SOURCE AND BINARY FORMS, WITH OR WITHOUT MODIFICATION, ARE PERMITTED
// PROVIDED THAT THE FOLLOWING CONDITIONS ARE MET:
// * REDISTRIBUTIONS OF SOURCE CODE MUST RETAIN THE ABOVE COPYRIGHT NOTICE
// * REDISTRIBUTIONS IN BINARY FORM MUST NOTE THE USE OF THE COMPONENT IN DOCUMENTATION AND/OR
//   OTHER MATERIALS PROVIDED WITH THE DISTRIBUTION.
// * NEITHER THE NAME OF TEAM MONGOOSE MAY BE USED TO ENDORES OR PROMOTE PRODUCTS
//   DERIVED FROM THIS SOFTWARE WITHOUT SPECIFIC PRIOR WRITTEN PERMISSION.
//
// IN NO EVENT SHALL TEAM MONGOOSE BE HELD LIABLE FOR INCIDENTAL, SPECIAL, INDIRECT,
// INCONSEQUENTIAL, UNBELIEVABLE, EXAGGERATED, VERBOSE, OR TYPICAL DAMAGES INCURRED WHILE USING
// THE ENCLOSED COMPONENTS.
//
// OUR STUFF ALWAYS WORKS - SOMETIMES.

#endregion

using Microsoft.AnalysisServices.AdomdClient;
using System.Data.Common;

namespace System.Data.MdxClient
{
    /// <summary>
    /// Represents a MDX transaction.
    /// </summary>
    public class MdxTransaction : DbTransaction
    {
        #region Fields

        private readonly MdxConnection connection;
        private readonly AdomdTransaction transaction;

        #endregion

        #region Properties

        /// <summary>
        /// Gets the internal, native ADOMD.NET transaction object.
        /// </summary>
        /// <value>An <see cref="AdomdTransaction"/> object.</value>
        protected internal AdomdTransaction AdomdTransaction
        {
            get
            {
                return this.transaction;
            }
        }

        /// <summary>
        /// Gets a value indicating whether the object has been disposed.
        /// </summary>
        /// <value>True if the object has been disposed; otherwise, false.</value>
        protected bool IsDisposed
        {
            get;
            private set;
        }

        #endregion

        #region Overrides

        /// <summary>
        /// Commits the transaction.
        /// </summary>
        public override void Commit()
        {
            this.transaction.Commit();
        }

        /// <summary>
        /// Gets the connection associated with the transaction.
        /// </summary>
        /// <value>A <see cref="MdxConnection"/> object.</value>
        protected override DbConnection DbConnection
        {
            get
            {
                return this.connection;
            }
        }

        /// <summary>
        /// Releases the managed and, optionally, the unmanaged resources used by the <see cref="MdxTransaction"/> class.
        /// </summary>
        /// <param name="disposing">Indicates whether the object is being disposed.</param>
        protected override void Dispose( bool disposing )
        {
            if ( this.IsDisposed )
                return;

            this.IsDisposed = true;
            this.transaction.Dispose();
            base.Dispose( disposing );
        }

        /// <summary>
        /// Gets the transaction isolation level.
        /// </summary>
        /// <value>One of the <see cref="IsolationLevel"/> values.</value>
        public override IsolationLevel IsolationLevel
        {
            get
            {
                return this.transaction.IsolationLevel;
            }
        }

        /// <summary>
        /// Rolls back the transaction.
        /// </summary>
        public override void Rollback()
        {
            this.transaction.Rollback();
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Instantiates a new instance of the <see cref="MdxTransaction"/> class.
        /// </summary>
        /// <param name="connection">The adapted <see cref="AdomdConnection"/> object.</param>
        /// <param name="transaction">The adapted <see cref="AdomdTransaction"/> object.</param>
        /// <exception cref="ArgumentNullException"><paramref name="connection"/> is null.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="transaction"/> is null.</exception>
        protected internal MdxTransaction( AdomdConnection connection, AdomdTransaction transaction )
            : this( new MdxConnection( connection ), transaction )
        {
        }

        /// <summary>
        /// Instantiates a new instance of the <see cref="MdxTransaction"/> class.
        /// </summary>
        /// <param name="connection">The <see cref="MdxConnection"/> associated with the transaction.</param>
        /// <param name="transaction">The adapted <see cref="AdomdTransaction"/> object.</param>
        /// <exception cref="ArgumentNullException"><paramref name="connection"/> is null.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="transaction"/> is null.</exception>
        protected internal MdxTransaction( MdxConnection connection, AdomdTransaction transaction )
        {
            if ( ( this.connection = connection ) == null )
                throw new ArgumentNullException( "connection" );

            if ( ( this.transaction = transaction ) == null )
                throw new ArgumentNullException( "transaction" );
        }

        #endregion
    }
}
